package com.spzx.JUC2.test4;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class Number {
    private Integer num = 0;

    private static final ReentrantLock reentrantLock = new ReentrantLock();
    //reentrantLock.newCondition();

    Condition condition = reentrantLock.newCondition();
    public  void add() throws InterruptedException {

        //1.上锁
        reentrantLock.lock();

        //2.判断
        while(num != 0){
            //this.wait();
            condition.await();
        };
        //3.执行
        num++;
        System.out.println(Thread.currentThread().getName() + num);

        //4.唤醒
        //this.notifyAll();
        condition.signalAll();
        //5.释放锁
        reentrantLock.unlock();

    }

    public void minus() throws InterruptedException {

        //1.上锁
        reentrantLock.lock();

        //2.判断
        while(num != 1){
            //this.wait();
            condition.await();
        };
        //3.执行
        num--;
        System.out.println(Thread.currentThread().getName() + num);

        //4.唤醒
        //this.notifyAll();
        condition.signalAll();
        //5.释放锁
        reentrantLock.unlock();

    }


}
